package com.ebelter.temperaturegun.ui.activity;


import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.RegisterResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CountDownTextView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.http.TGNetUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class RegisterActivity extends TBaseActivity {

    private static final String TAG = "RegisterActivity";
    public static final int RequestCode = 45;
    public static final int ResultCode = 68;
    private ImageView top_iv;
    private TextView top_tv1;
    private TextView top_tv2;

    private TextView tv_birthday_register;

    private EditText et_name_register;
    private EditText et_email_register;
    private EditText et_emailcode_register;
    private CountDownTextView countDownTextView;
    private EditText et_pwd_register;
    private EditText et_repwd_register;

    @Override
    protected int setContentViewId() {
        return R.layout.activity_register;
    }

    private String birthday;

    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv2=findViewById(R.id.top_tv2);

        et_name_register=findViewById(R.id.et_name_register);
        et_email_register=findViewById(R.id.et_email_register);
        countDownTextView=findViewById(R.id.register_countDownTv);
        et_pwd_register=findViewById(R.id.et_pwd_register);
        et_repwd_register=findViewById(R.id.et_repwd_register);
        et_emailcode_register=findViewById(R.id.et_emailcode_register);

        tv_birthday_register=findViewById(R.id.tv_birthday_register);
        tv_birthday_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectBirthday();
            }
        });

        //返回键
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        countDownTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countDownTextViewClick();
            }
        });



        tempBirthday="1990-06-30 10:00:00";
        //tv_birthday_register.setText(birthday.substring(0,10));

    }


    private String tempBirthday;
    private void selectBirthday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker= new CustomDatePicker(this,new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG,time);
                //dateStr = time.substring(0,10);
                tempBirthday = time;

            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);
        datePicker.setSelectedTime(tempBirthday);


        datePicker.setBackgroundColor(Color.parseColor("#0176ff"));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                birthday=tempBirthday;
                ViewUtils.setTextViewStr(tv_birthday_register,birthday.substring(0,10));

            }
        });

    }

    private void countDownTextViewClick() {

        String email=et_email_register.getText().toString().trim();
        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
        }else {

            if (!NetUtils.available()){
                ToastUtil.show(R.string.Network_error);
                return;
            }

            //showLoadingDialog(StringUtils.getResStr(R.string.Request_email_verification_code));
            TGNetUtils.getInstance().getEmailValidateCodeRequest(this, email, 1, new HttpResponse<getEmailValidateCodeResponse>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse,String s) {
                    // cancelLoadingDialog();
                    LogUtils.i(TAG,"-- s = " + s);
                    if (isRequestSuccess){

                        if (getEmailValidateCodeResponse.resultCode==0){
                            if (countDownTextView!=null)countDownTextView.click();//说明请求成功，按钮进入倒计时模式
                            ToastUtil.show(R.string.The_email_has_been_sent);
                        }else if (getEmailValidateCodeResponse.resultCode==102){//邮箱已经注册
                            ToastUtil.show(R.string.Email_has_been_registered);
                        }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                            ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                        }else {
                            ToastUtil.show(R.string.Failed_to_send_mail);
                        }

                    }else {
                        ToastUtil.show(R.string.Network_connection_error);
                    }
                }
            });
        }

    }


    //注册
    public void register(View view) {



//        ToastUtil.show(R.string.Registered_successfully);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent=new Intent();
//                intent.putExtra("n","zcqzz");
//                intent.putExtra("p","123456");
//                setResult(ResultCode,intent);
//                finish();
//            }
//        },1000);



        final String nickName = et_name_register.getText().toString().trim();
        String email = et_email_register.getText().toString().trim();
        final String password = et_pwd_register.getText().toString().trim();
        String password2 = et_repwd_register.getText().toString().trim();
        String code = et_emailcode_register.getText().toString().trim();

        if (TextUtils.isEmpty(nickName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }

        if (nickName.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        if (TextUtils.isEmpty(code)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        String birthday = tv_birthday_register.getText().toString().trim();
        if (TextUtils.isEmpty(birthday)){
            ToastUtil.show(R.string.shrbnwk);
            return;
        }

        if (TextUtils.isEmpty(password)){
            ToastUtil.show(R.string.The_Password_cannot_be_empty);
            return;
        }

        if (password.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (password.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!TextUtils.equals(password2,password)){
            ToastUtil.show(R.string.The_password_is_inconsistency_twice);//前后密码输入不一致
            return;
        }


        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        showLoadingDialog("");



        TGNetUtils.getInstance().registerRequest(this, nickName, email, MD5Utils.getMd5(password), birthday,
                0, 0, 0, 0, code, 1, new HttpResponse<RegisterResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestDes, RegisterResponse registerResponse, String s) {
                        LogUtils.i(TAG,"-- s = " + s);
                        dissLoadingDialog();
                        LogUtils.i(TAG,registerResponse!=null?registerResponse.toString():"");

                        if (isRequestSuccess&&registerResponse!=null){


                            if (registerResponse.resultCode==0){
                                LogUtils.i(TAG,"--------注册成功");
                                ToastUtil.show(R.string.Registered_successfully);
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent();
                                        intent.putExtra("n",nickName);
                                        intent.putExtra("p",password);
                                        setResult(ResultCode,intent);
                                        finish();
                                    }
                                },1000);
                            }else if (registerResponse.resultCode==106){
                                ToastUtil.show(R.string.Email_verification_code_has_expired);
                            }else if (registerResponse.resultCode==107){
                                ToastUtil.show(R.string.Wrong_email_verification_code);
                            }else if (registerResponse.resultCode==103){
                                ToastUtil.show(R.string.The_email_or_nickname_has_been_registered);
                            } else if (registerResponse.resultCode==110){
                                ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                            } else {
                                ToastUtil.show(R.string.Registration_failed);
                            }


                        }else {
                            LogUtils.i(TAG,"----注册失败");
                        }

                    }
                });


    }
}
